import urllib.request
import urllib.parse
from http.client import HTTPResponse

# 1.请求对象，编码
params = {
    "ie": "UTF-8",
    "wd": "周杰伦"
}
url = "https://www.baidu.com/s?" + urllib.parse.urlencode(params)
headers = {
    'Cookie': 'BDUSS_BFESS=XpZT1FVWGhYd3ZFMncwVW55WmZmQUZ1eEVlRm5IVmYwQlVUSTdMR3BiV2cxd1ZtRVFBQUFBJCQAAAAAAAAAAAEAAACNt7Q0emR6aDAwMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKBK3mWgSt5lQ;',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0',
}
request = urllib.request.Request(url, data=urllib.parse.urlencode(params).encode("utf8"), headers=headers)
# 2.发送请求，响应
opener = urllib.request.build_opener()
response = opener.open(request)  # type:HTTPResponse
# response = urllib.request.urlopen(request)  # type:HTTPResponse

# 3.读取数据，解码
# getcode geturl getheader read readline readlines
print(response.read().decode("utf-8"))
